<template>
  <el-row style="height: 100vh" class="box">
    <el-col :xs="8" :md="12" :lg="16" class="left">
      <h1>欢&nbsp;迎&nbsp;登&nbsp;录</h1>
      <h2 class="title">xxxx后台管理系统</h2>
    </el-col>
    <el-col :xs="16" :md="12" :lg="8" class="right" :class="login">
      <h2 class="user">账号登录</h2>
      <el-form
        ref="formRef"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item prop="username" class="name">
          <el-input
            v-model="formInline.username"
            placeholder="请输入用户名"
            class="username"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="pwd">
          <el-input
            v-model="formInline.password"
            type="password"
            show-password
            class="password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loadingLogin"
            type="primary"
            style="width: 100%"
            @click="onSubmit"
            class="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import { login } from "@/api/manager.js";
import { toast } from "@/utils/index.js";
import { setToken } from "@/utils/auth.js";
const loadingLogin = ref(false)
const router = useRouter();
const formInline = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [
    {
      required: true,
      message: "请输入正确的用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入正确的密码",
      trigger: "blur",
    },
  ],
});
const formRef = ref(null);
const onSubmit = () => {
  formRef.value.validate((res) => {
    if (!res) {
     return false;
    }
    loadingLogin.value = true; 
    login(formInline.username, formInline.password)
        .then((res) => {
          toast("登录成功！");
          const token = res.token;
          setToken(token);
          router.push({
            path: "/",
          });
          // router.push("/");
        })
        .catch((err) => {
          // toast(err.response.data.msg, "error");
          console.log(err, "error");
        })
        .finally(()=>{
          loadingLogin.value = false;
        })
  });
};
function keyUp(event) {
  if (event.keyCode === 13) onSubmit();
}
onMounted(() => {
  window.addEventListener("keyup", keyUp);
});
onUnmounted(() => {
  window.removeEventListener("keyup", keyUp);
});
</script>
<style scoped>
.box {
  background: url("../image/bg3.jpeg") 0 0 no-repeat;
  background-size: cover;
}
h1 {
  font-size: 70px;
}
.left {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  color: #fff;
}
.right {
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(3px);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.username {
  width: 400px;
  height: 40px;
  font-size: 18px;
}
.password {
  width: 400px;
  height: 40px;
  font-size: 18px;
  margin: 10px 0px;
}
.login {
  width: 400px;
  height: 40px;
  font-size: 20px;
}
.user {
  font-size: 32px;
  color: rgb(64, 158, 255);
  padding-bottom: 10px;
}
.title {
  font-size: 50px;
}
</style>
